#include "my_inc/main_login.h"
#include "my_inc/tic.h"

// ＃界面
void tic(face_p head)
{
    face_p temp        = faceinsertend(head, "井字棋");
    lv_obj_t * obj_tic = lv_obj_create(temp->screen);
    lv_obj_set_size(obj_tic, 800, 480);
    lv_obj_set_style_bg_color(obj_tic, lv_color_hex(0xc1c1c1), LV_PART_MAIN);

    // 定义网格列和行的尺寸数组
    static lv_coord_t col_dsc[] = {180, 180, 180, 180, LV_GRID_TEMPLATE_LAST}; // col_dsc: 定义4列，每列宽度70像素
    static lv_coord_t row_dsc[] = {180, 180, 65, LV_GRID_TEMPLATE_LAST};       // row_dsc: 定义2行，每行高度50像素
    lv_obj_t * cont             = lv_obj_create(temp->screen);                 // 创建容器对象
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);                           // ⭐️ 关键代码：禁用滚动
    lv_obj_set_style_bg_color(cont, lv_color_hex(0xFFFFFF), LV_PART_MAIN);     // 设置背景色
    lv_obj_set_style_bg_opa(cont, LV_OPA_0, LV_PART_MAIN);                     // 只设置背景的透明度
    lv_obj_set_style_grid_column_dsc_array(cont, col_dsc, 0);                  // 设置列和行的尺寸数组
    lv_obj_set_style_grid_row_dsc_array(cont, row_dsc, 0);                     // 设置列和行的尺寸数组
    lv_obj_set_size(cont, 800, 480);                                           // 设置容器大小(800x440)
    lv_obj_align(cont, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_layout(cont, LV_LAYOUT_GRID); // 设置布局为网格布局 LV_LAYOUT_GRID

    // 定义网格列和行的尺寸数组
    static lv_coord_t col_dsc_tic[] = {132, 132, 132, LV_GRID_TEMPLATE_LAST};  // col_dsc: 定义4列，每列宽度70像素
    static lv_coord_t row_dsc_tic[] = {132, 132, 132, LV_GRID_TEMPLATE_LAST};  // row_dsc: 定义2行，每行高度50像素
    lv_obj_t * cont_tic             = lv_obj_create(temp->screen);             // 创建容器对象
    lv_obj_clear_flag(cont_tic, LV_OBJ_FLAG_SCROLLABLE);                       // ⭐️ 关键代码：禁用滚动
    lv_obj_set_style_bg_color(cont_tic, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 设置背景色
    lv_obj_set_style_bg_opa(cont_tic, LV_OPA_0, LV_PART_MAIN);                 // 只设置背景的透明度
    lv_obj_set_style_grid_column_dsc_array(cont_tic, col_dsc_tic, 0);          // 设置列和行的尺寸数组
    lv_obj_set_style_grid_row_dsc_array(cont_tic, row_dsc_tic, 0);             // 设置列和行的尺寸数组
    lv_obj_set_style_pad_column(cont_tic, 0, LV_PART_MAIN);                    // 设置网格容器中列与列之间的间距
    lv_obj_set_style_pad_row(cont_tic, 0, LV_PART_MAIN);                       // 设置网格容器中行与行之间的间距
    lv_obj_set_size(cont_tic, 396, 396);                                       // 设置容器大小(800x440)
    lv_obj_align(cont_tic, LV_ALIGN_TOP_MID, 0, 0);
    // lv_obj_set_style_pad_all(cont_tic, 0, LV_PART_MAIN);        // 去除内边距
    lv_obj_set_style_pad_all(cont_tic, 2, LV_PART_MAIN); // 容器内边距2px
    lv_obj_set_layout(cont_tic, LV_LAYOUT_GRID);         // 设置布局为网格布局 LV_LAYOUT_GRID
    lv_obj_set_grid_cell(cont_tic, LV_GRID_ALIGN_STRETCH, 1, 3, LV_GRID_ALIGN_STRETCH, 0, 3);

    int count = 0;
    init_tic();
    main_tic_p tic_p = malloc(sizeof(main_tic_t));
    tic_p->cont_tic  = cont_tic;
    tic_p->a[0]      = '0';
    tic_p->a[1]      = 'X';
    tic_p->temp      = 0;
    tic_p->head      = head;
    for(size_t i = 0; i < 3; i++) {
        for(size_t j = 0; j < 3; j++) {
            lv_obj_t * obj = lv_obj_create(cont_tic);
            lv_obj_set_style_border_width(obj, 2, LV_PART_MAIN); // 边框宽度
            lv_obj_set_style_border_color(obj, lv_color_hex(0x776e65), LV_PART_MAIN);
            lv_obj_set_style_radius(obj, 4, LV_PART_MAIN);
            lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, j, 1, LV_GRID_ALIGN_STRETCH, i, 1);

            lv_obj_t * labell = lv_label_create(obj);
            char num_str[10];
            snprintf(num_str, sizeof(num_str), "%d", ++count);
            lv_label_set_text(labell, num_str);
            lv_obj_add_flag(labell, LV_OBJ_FLAG_HIDDEN); // 隐藏OBJ
            lv_obj_add_event_cb(obj, tic_cell, LV_EVENT_CLICKED, labell);

            lv_obj_t * label = lv_label_create(obj);
            char text[2]     = {sign[i][j], '\0'}; // 将字符转换为字符串
            lv_label_set_text(label, text);
            lv_obj_center(label);
        }
    }

    // -------------------------- 3. 底部：按钮区域（保持不变） --------------------------
    lv_obj_t * obj = lv_obj_create(cont);
    tic_p->obj_j   = 0;
    tic_p->obj     = obj;
    lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);       // 隐藏OBJ
    lv_obj_set_style_pad_all(obj, 0, LV_PART_MAIN); // 去掉默认的内补 20
    lv_obj_set_style_grid_cell_y_align(obj, LV_ALIGN_BOTTOM_MID, 0);
    // 向上偏移10像素
    // lv_obj_set_style_margin_top(obj, -10, 0);

    // 按钮区域横跨cont的2列（左右都占），放在第2行（底部60px）
    lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 4, LV_GRID_ALIGN_STRETCH, 2, 1);
    lv_obj_set_style_bg_color(obj, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(obj, LV_OPA_30, LV_PART_MAIN);

    lv_obj_t * btn_home = lv_btn_create(obj);
    lv_obj_center(btn_home);
    lv_obj_t * home = lv_label_create(btn_home);
    lv_label_set_text(home, LV_SYMBOL_HOME);
    lv_obj_add_event_cb(btn_home, home_tic, LV_EVENT_CLICKED, tic_p);

    lv_obj_t * btn_close = lv_btn_create(obj);
    lv_obj_align(btn_close, LV_ALIGN_RIGHT_MID, -100, 0);
    lv_obj_t * close = lv_label_create(btn_close);
    lv_label_set_text(close, LV_SYMBOL_POWER);

    lv_obj_t * btn_return = lv_btn_create(obj);
    lv_obj_align(btn_return, LV_ALIGN_LEFT_MID, 100, 0);
    lv_obj_t * label_return = lv_label_create(btn_return);
    lv_label_set_text(label_return, LV_SYMBOL_LEFT);
    lv_obj_add_event_cb(btn_return, home_tic, LV_EVENT_CLICKED, tic_p);

    lv_obj_t * btn_anew = lv_btn_create(temp->screen);
    lv_obj_align(btn_anew, LV_ALIGN_TOP_RIGHT, -80, 20);
    lv_obj_t * label_anew = lv_label_create(btn_anew);
    lv_label_set_text(label_anew, LV_SYMBOL_REFRESH);
    lv_obj_add_event_cb(btn_anew, reset_tic, LV_EVENT_CLICKED, tic_p);

    lv_obj_t * btn_re = lv_btn_create(temp->screen);
    lv_obj_align(btn_re, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_obj_t * label_menu = lv_label_create(btn_re);
    lv_label_set_text(label_menu, LV_SYMBOL_LIST);
    lv_obj_add_event_cb(btn_re, menu_tic, LV_EVENT_CLICKED, tic_p);

    // 显示游戏结束提示（但不覆盖棋盘）
    lv_obj_t * game_over_label = lv_label_create(temp->screen);
    tic_p->game_over_label     = game_over_label;
    lv_obj_add_flag(game_over_label, LV_OBJ_FLAG_HIDDEN); // 隐藏OBJ
    lv_obj_align(game_over_label, LV_ALIGN_CENTER, 0, 0); // 显示在棋盘中间
    lv_obj_set_style_text_font(game_over_label, &SourceHanSansCN_Bold_20, 0);
    lv_obj_set_style_text_color(game_over_label, lv_color_hex(0xFF0000), 0);
    lv_obj_set_style_bg_color(game_over_label, lv_color_hex(0xFFFFFF), 0);
    lv_obj_set_style_bg_opa(game_over_label, LV_OPA_70, 0);
    lv_obj_set_style_pad_all(game_over_label, 10, 0);

    // 创建游戏更新定时器（每100ms检查一次）
    game_timer = lv_timer_create(main_tic, 500, tic_p);
    lv_timer_set_repeat_count(game_timer, -1); // 无限重复
}
